import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '@/utils/token.js'

const asyncRouter = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/Home/Home.vue'),
    meta: {
      isShowNav: true,
    },
  },
  {
    path: '/recommend',
    name: 'recommend',
    component: () => import('@/views/Recommend/Recommend.vue'),
    meta: {
      isShowNav: true,

      needLogin: true  //表示该路由 需要登录才能访问
    },
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/Cart/Cart.vue'),
    meta: {
      isShowNav: true,
      title: '购物车'
    },
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('@/views/User/User.vue'),
    meta: {
      isShowNav: true,
      title: "我的"
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue'),
    meta: {
      isShowNav: false,
    },
  },
  {
    // 动态路由
    path: '/goods/:id',
    name: 'goods',
    component: () => import('@/views/Home/GoodsDetail.vue'),
    meta: {
      title: '商品详情'
    }
  },

  {
    path: '/address-list',
    name: 'addressList',
    component: () => import('@/views/Address/Address.vue'),
    meta: {
      login: true,   //访问是否需要登录
      title: '收货地址',  //顶部标题
    },
  },
  {
    path: '/edit-address',
    name: 'editressList',
    component: () => import('@/views/Address/EditAddress.vue'),
    meta: {
      login: true,
      //title: '收货地址编辑',
    },
  },
  {
    path: '/create-order',
    name: 'createOrder',
    component: () => import('@/views/Order/Created.vue'),
    meta: {
      login: true,
      title: '生成订单'
    },
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('@/views/Order/List.vue'),
    meta: {
      title: '我的订单',
      login: true,
    },
  },
  {
    path: '/add-article',
    name: 'addArticle',
    component: () => import('@/views/Recommend/Add.vue'),
    meta: {
      title: '新增种草文章',
      login: true,
    },
  },
  {
    path: '/article-detail',
    name: 'articleDetail',
    component: () => import('@/views/Recommend/Detail.vue'),
    meta: {
      title: '种草文章详情',
      login: true,
    },
  },
  {
    path: '/my-article',
    name: 'myArticle',
    component: () => import('@/views/Recommend/Recommend.vue'),
    meta: {
      //title: '我的文章',
      login: true,
    },
  },
  {
    path: '/thumbs-up',
    name: 'ThumbsUp',
    component: () => import('@/views/User/ThumbsUp.vue'),
    meta: {
      title: '我的点赞',
      login: true
    }
  },
  {
    path: '/my-collection',
    name: 'myCollection',
    component: () => import('@/views/User/Collection.vue'),
    meta: {
      title: '我的点赞',
      login: true
    }
  },


  // 访问没有的路由直接跳往首页
  {
    path: '/:toHome*',
    name: 'toHome',
    redirect: '/home',
  },
]
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [...asyncRouter]
})


// 全局 路由 前置守卫
router.beforeEach((to, from, next) => {
  // 先判断是否登录
  if (getToken()) {
    // 已经登录
    if (to.path === '/login') {
      next(from.path)
    } else {
      next()
    }
  } else {
    // 未登录
    if (to.meta.needLogin) {
      // to.meta.needLogin 表示对应路由是否需要登录
      next('/login')
    } else {
      next()
    }
  }

})

export default router
